package com.qipay.sp.excutor.qyhb2;

import com.qipay.baas.model.AlipayTransferLog;
import com.qipay.baas.model.AlipayTransferLogExample;
import com.qipay.baas.model.Order;
import com.qipay.mapper.AlipayTransferLogMapper;
import com.qipay.order.config.OrderStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.scalar.type.ApiStatusEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class Qyhb2NativeExcutor extends AbstractExcutor {
    @Autowired
    AlipayTransferLogMapper alipayTransferLogMapper;
    @Autowired
    OrderService orderService;
    @Value("${api.createQrCode.url}")
    private String createQrCodeUrl;

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("orderNo", order.getTradeNo());
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        Order order = orderService.getByTradeNo(params.get("orderNo").toString());
        //根据spid，时间，金额来查找五分钟内是否有等待支付订单，如果有，返回失败
        List<Order> orders = orderService.selectBySpidAmountTime(order.getSpId(),order.getAmount());
        if(orders!=null){
            //查找对应的支付链接
            String qr_code_url = findQrCode(order.getSpId(),order.getAmount());
            if(StringUtils.isBlank(qr_code_url))qr_code_url = findQrCode(order.getSpId(),0);
            if(StringUtils.isBlank(qr_code_url)){
                resultMap.put("status", ApiStatusEnum.FIAL.getCode());
                resultMap.put("message", "没有找到对应金额的二维码");
                order.setState(OrderStateEnum.NONEQRCODE.getType());
            }else {
                order.setAttach(qr_code_url);
                resultMap.put("status", ApiStatusEnum.OK.getCode());
                resultMap.put("pay_info", createQrCodeUrl+"?tradeNo="+order.getTradeNo());
                order.setState(OrderStateEnum.WAITING.getType());
            }
        }else {
            //五分钟内有未支付订单，订单失败
            resultMap.put("status", ApiStatusEnum.FIAL.getCode());
            resultMap.put("message", "下单失败");//号上太少了
            order.setState(OrderStateEnum.REQERROR.getType());
        }
        /**五分钟内匹配订单去掉，这是测试通道随便测试*/
        /*if(orders!=null&&orders.size()==1){
            //查找对应的支付链接
            String qr_code_url = findQrCode(order.getSpId(),order.getAmount());
            if(StringUtils.isBlank(qr_code_url))qr_code_url = findQrCode(order.getSpId(),0);
            if(StringUtils.isBlank(qr_code_url)){
                resultMap.put("status", ApiStatusEnum.FIAL.getCode());
                resultMap.put("message", "没有找到对应金额的二维码");
                order.setState(OrderStateEnum.NONEQRCODE.getType());
            }else {
                order.setAttach(qr_code_url);
                resultMap.put("status", ApiStatusEnum.OK.getCode());
                resultMap.put("pay_info", createQrCodeUrl+"?tradeNo="+order.getTradeNo());
                order.setState(OrderStateEnum.WAITING.getType());
            }
        }else {
            //五分钟内有未支付订单，订单失败
            resultMap.put("status", ApiStatusEnum.FIAL.getCode());
            resultMap.put("message", "无个码通道可分配");//号上太少了
            order.setState(OrderStateEnum.NONEALIPAY.getType());
        }*/
        orderService.update(order);
        return resultMap;
    }

    /**
     * 找对应金额的二维码
     * @param spId
     * @param amount
     * @return
     */
    private String findQrCode(Long spId,Integer amount){
        String qr_code_url = null;
        AlipayTransferLogExample alipayTransferLogExample = new AlipayTransferLogExample();
        alipayTransferLogExample.createCriteria().andSpIdEqualTo(spId).andAmountEqualTo(amount);
        List<AlipayTransferLog> alipayTransferLogs = alipayTransferLogMapper.selectByExample(alipayTransferLogExample);
        if(alipayTransferLogs!=null&&alipayTransferLogs.size()>0) {
            qr_code_url = alipayTransferLogs.get(0).getTradeNo();
        }
        return qr_code_url;
    }

}
